/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package utopiaintelpackage.sos;

import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import main.CommonMethods;

import org.jibble.pircbot.Colors;

import tools.UtopiaTime;
import utopiaintelpackage.AbstractIntel;

/**
 * This class represents a SoS The class offers convenience methods for adding and removing commonly used parameters
 * 
 * 
 */
public class SoS extends AbstractIntel {
   private final int amountOfSciences;
   private int totalSciencePoints;

   public SoS(final int amountOfSciences) {
	  super();
	  this.amountOfSciences = amountOfSciences;
   }

   public String getExportLine() {
	  if (getParameter("export") == null)
		 return "";
	  return (String) getParameter("export");
   }

   @Override
   public List<String> getIRCOutput(final boolean shortVersion) {
	  List<String> out = new LinkedList<String>();
	  String adderPers = CommonMethods.getPersonality(getSavedBy());
	  out.add(">> " + Colors.OLIVE + getName() + getKdloc() + Colors.NORMAL + " << " + Colors.OLIVE + getRace() + " "
			+ getPersonality() + Colors.NORMAL
			+ (adderPers.equals("Tactician") ? Colors.BOLD + " Tactician-" + Colors.NORMAL : " ") + "SoS added: "
			+ UtopiaTime.compareTimes(getUpdated().getTime()) + " ago"
			+ ("".equals(getSavedBy()) ? "" : " by " + Colors.NORMAL + "o" + Colors.RED + getSavedBy() + Colors.NORMAL + "o"));

	  if (shortVersion) {
		 out.add("Export:" + Colors.DARK_GREEN + " " + getExportLine());
	  } else {
		 String sciences = getSciences();
		 String[] split = sciences.split("\n");

		 int maxName = 0;
		 int maxAmount = 0;
		 int maxEffect = 0;

		 for (final String s : split) {
			String[] tabSplit = s.split("\t");
			if (tabSplit[0].length() > maxName) {
			   maxName = tabSplit[0].length() + 1;
			}
			if (tabSplit[1].length() > maxAmount) {
			   maxAmount = tabSplit[1].length() + 3;
			}
			if (tabSplit[2].length() > maxEffect) {
			   maxEffect = tabSplit[2].length();
			}
		 }

		 int perRow = 1;
		 String row = "";
		 for (final String s : split) {
			String[] tabSplit = s.split("\t");
			if (perRow == 3) {
			   out.add(row);
			   perRow = 1;
			   row = "";
			}
			String name = String.format("%1$-" + maxName + "s", tabSplit[0] + ":");
			String amount = String.format("%1$" + maxAmount + "s", "(" + tabSplit[1] + ")");
			String effect = String.format("%1$" + maxEffect + "s", tabSplit[2]);

			row += Colors.BLUE + name + " " + Colors.OLIVE + effect + " " + Colors.DARK_GREEN + amount + " ";
			++perRow;
		 }
		 if (!"".equals(row)) {
			out.add(row);
		 }
		 out.add(Colors.BLUE + "Total: " + Colors.OLIVE + getTotalSciencePoints() + Colors.BLUE + " BPA: " + Colors.OLIVE
			   + getTotalSciencePoints() / getLand());
		 if (!getExportLine().equals("")) {
			out.add("Export:" + Colors.DARK_GREEN + " " + getExportLine());
		 }
	  }
	  return out;
   }

   public String getKdloc() {
	  if (getParameter("kdloc") == null)
		 return "";
	  return (String) getParameter("kdloc");
   }

   public Integer getLand() {
	  if (getParameter("land") == null)
		 return 0;
	  return (Integer) getParameter("land");
   }

   public String getName() {
	  if (getParameter("name") == null)
		 return "";
	  return (String) getParameter("name");
   }

   public String getPersonality() {
	  if (getParameter("personality") == null)
		 return "";
	  return (String) getParameter("personality");
   }

   public String getRace() {
	  if (getParameter("race") == null)
		 return "";
	  return (String) getParameter("race");
   }

   @Override
   public Map<String, Object> getRPCOutput(final int... args) {
	  Map<String, Object> out = new HashMap<String, Object>();
	  out.put("Name", getName());
	  out.put("KD", getKdloc());
	  out.put("Race", getRace());
	  out.put("Personality", getPersonality());
	  out.put("Land", getLand());
	  Map<String, String> points = new HashMap<String, String>();
	  Map<String, String> effects = new HashMap<String, String>();
	  for (final String s : getSciences().split("\n")) {
		 String[] tabSplit = s.split("\t");
		 points.put(tabSplit[0], tabSplit[1]);
		 effects.put(tabSplit[0], tabSplit[2]);
	  }
	  out.put("Science Points", points);
	  out.put("Science Effects", effects);
	  out.put("Total Points", getTotalSciencePoints());
	  out.put("Export Line", getExportLine());
	  out.put("Saved By", getSavedBy());
	  out.put("Added Time", getUpdated());
	  return out;
   }

   public String getSavedBy() {
	  if (getParameter("savedby") == null)
		 return "";
	  return (String) getParameter("savedby");
   }

   public String getScienceEffect(final String sciencetype) {
	  if (getParameter(sciencetype) == null)
		 return "";
	  return (String) getParameter(sciencetype);
   }

   public int getSciencePoints(final String sciencetype) {
	  if (getParameter(sciencetype) == null)
		 return 0;
	  return (Integer) getParameter(sciencetype);
   }

   public String getSciences() {
	  if (getParameter("sciences") == null)
		 return "";
	  return (String) getParameter("sciences");
   }

   public int getTotalSciencePoints() {
	  return totalSciencePoints;
   }

   @Override
   public String getTypeOfIntel() {
	  return "SoS";
   }

   public Date getUpdated() {
	  return (Date) getParameter("updated");
   }

   @Override
   public boolean isComplete() {
	  return getIntel().size() > 2 + 2 * amountOfSciences && getParameter("name") != null && getParameter("kdloc") != null
			&& getParameter("updated") != null;
   }

   public void setExportLine(final String exportLine) {
	  setParameter("export", exportLine);
   }

   public void setKdloc(final String kdloc) {
	  setParameter("kdloc", kdloc);
   }

   public void setLand(final int land) {
	  setParameter("land", land);
   }

   public void setName(final String name) {
	  setParameter("name", name);
   }

   public void setPersonality(final String personality) {
	  setParameter("personality", personality);
   }

   public void setRace(final String race) {
	  setParameter("race", race);
   }

   public void setSavedBy(final String savedBy) {
	  setParameter("savedby", savedBy);
   }

   public void setScienceEffect(final String sciencetype, final String effect) {
	  setParameter(sciencetype + " effect", effect);
   }

   public void setSciencePoints(final String sciencetype, final int points) {
	  setParameter(sciencetype + " points", points);
	  setTotalSciencePoints(getTotalSciencePoints() + points);
   }

   public void setSciences(final String sciences) {
	  setParameter("sciences", sciences);
   }

   public void setTotalSciencePoints(final int sciencePoints) {
	  totalSciencePoints = sciencePoints;
   }

   public void setUpdated(final Date updated) {
	  setParameter("updated", updated);
   }
}
